/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ArrayUtil;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.Tag;
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImapFlagCache
/*     */   implements Iterable<ImapFlag>, Serializable
/*     */ {
/*     */   private static final long serialVersionUID = -8938341239505513246L;
/*     */   private final Map<String, ImapFlag> mImapNames;
/*     */   private final Map<String, ImapFlag> mNames;
/*     */   
/*     */   public static final class ImapFlag
/*     */     implements Serializable
/*     */   {
/*     */     private static final long serialVersionUID = 5445749167572465447L;
/*     */     final String mName;
/*     */     final String mImapName;
/*     */     final int mId;
/*     */     final long mBitmask;
/*     */     final boolean mPositive;
/*     */     final boolean mPermanent;
/*     */     final boolean mListed;
/*     */     final int mModseq;
/*     */     static final boolean VISIBLE = true;
/*     */     static final boolean HIDDEN = false;
/*     */     
/*     */     ImapFlag(Tag ltag)
/*     */     {
/*  51 */       this(ltag.getName(), ltag, true);
/*     */     }
/*     */     
/*     */     ImapFlag(String name, Tag ltag, boolean positive) {
/*  55 */       this.mId = ltag.getId();this.mBitmask = ((ltag instanceof Flag) ? ((Flag)ltag).toBitmask() : 0L);
/*  56 */       this.mName = ltag.getName();this.mImapName = normalize(name, this.mId);
/*  57 */       this.mPositive = positive;this.mPermanent = true;
/*  58 */       this.mListed = true;this.mModseq = ltag.getSavedSequence();
/*     */     }
/*     */     
/*     */     ImapFlag(String name, short bitmask, boolean listed) {
/*  62 */       this.mId = 0;this.mBitmask = bitmask;
/*  63 */       this.mName = name;this.mImapName = name;
/*  64 */       this.mPositive = true;this.mPermanent = false;
/*  65 */       this.mListed = listed;this.mModseq = -1;
/*     */     }
/*     */     
/*     */     private String normalize(String name, int id) {
/*  69 */       StringBuilder sb = new StringBuilder(name.length());
/*  70 */       for (int i = 0; i < name.length(); i++) {
/*  71 */         char c = name.charAt(i);
/*     */         
/*  73 */         if ((c > ' ') && (c < '') && (c != '(') && (c != ')') && (c != '{') && (c != '%') && (c != '*') && (c != '"') && (c != ']') && ((i == 0) || (c != '\\'))) {
/*  74 */           sb.append(c);
/*     */         }
/*     */       }
/*     */       
/*  78 */       if (sb.length() != name.length()) {
/*  79 */         sb.append(":FLAG").append(id - 64);
/*     */       }
/*  81 */       return sb.toString();
/*     */     }
/*     */     
/*     */     boolean matches(ImapMessage i4msg) {
/*  85 */       if (this.mId > 0) {
/*  86 */         String[] tags = i4msg.tags;
/*  87 */         if (!ArrayUtil.isEmpty(tags)) {
/*  88 */           for (String tag : tags) {
/*  89 */             if (this.mName.equals(tag)) {
/*  90 */               return true;
/*     */             }
/*     */           }
/*     */         }
/*  94 */         return false;
/*     */       }
/*  96 */       long mask = this.mId == 0 ? i4msg.sflags : i4msg.flags;
/*  97 */       return (mask & this.mBitmask) != 0L;
/*     */     }
/*     */     
/*     */ 
/*     */     public String toString()
/*     */     {
/* 103 */       return this.mImapName;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public ImapFlagCache()
/*     */   {
/* 112 */     this.mImapNames = Maps.newLinkedHashMap();
/* 113 */     this.mNames = Maps.newHashMap();
/*     */   }
/*     */   
/*     */   ImapFlagCache(Mailbox mbox, OperationContext octxt) throws ServiceException {
/* 117 */     this();
/*     */     try {
/* 119 */       for (Tag ltag : mbox.getTagList(octxt)) {
/* 120 */         if (!(ltag instanceof Flag)) {
/* 121 */           cache(new ImapFlag(ltag));
/*     */         }
/*     */       }
/*     */     } catch (ServiceException e) {
/* 125 */       if (!e.getCode().equals("service.PERM_DENIED")) {
/* 126 */         throw e;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   static ImapFlagCache getSystemFlags(Mailbox mbox) throws ServiceException {
/* 132 */     ImapFlagCache i4cache = new ImapFlagCache();
/*     */     
/* 134 */     i4cache.cache(new ImapFlag("\\Answered", mbox.getFlagById(Flag.ID_REPLIED), true));
/* 135 */     i4cache.cache(new ImapFlag("\\Deleted", mbox.getFlagById(Flag.ID_DELETED), true));
/* 136 */     i4cache.cache(new ImapFlag("\\Draft", mbox.getFlagById(Flag.ID_DRAFT), true));
/* 137 */     i4cache.cache(new ImapFlag("\\Flagged", mbox.getFlagById(Flag.ID_FLAGGED), true));
/* 138 */     i4cache.cache(new ImapFlag("\\Seen", mbox.getFlagById(Flag.ID_UNREAD), false));
/* 139 */     i4cache.cache(new ImapFlag("$Forwarded", mbox.getFlagById(Flag.ID_FORWARDED), true));
/* 140 */     i4cache.cache(new ImapFlag("$MDNSent", mbox.getFlagById(Flag.ID_NOTIFIED), true));
/* 141 */     i4cache.cache(new ImapFlag("Forwarded", mbox.getFlagById(Flag.ID_FORWARDED), true));
/*     */     
/* 143 */     i4cache.cache(new ImapFlag("\\Recent", (short)1, false));
/* 144 */     i4cache.cache(new ImapFlag("$Junk", (short)2, true));
/* 145 */     i4cache.cache(new ImapFlag("$NotJunk", (short)4, true));
/* 146 */     i4cache.cache(new ImapFlag("Junk", (short)2, true));
/* 147 */     i4cache.cache(new ImapFlag("JunkRecorded", (short)8, true));
/* 148 */     i4cache.cache(new ImapFlag("NonJunk", (short)4, true));
/* 149 */     i4cache.cache(new ImapFlag("NotJunk", (short)4, true));
/*     */     
/* 151 */     return i4cache;
/*     */   }
/*     */   
/*     */   ImapFlag getByImapName(String name)
/*     */   {
/* 156 */     return (ImapFlag)this.mImapNames.get(name.toUpperCase());
/*     */   }
/*     */   
/*     */   ImapFlag getByZimbraName(String name) {
/* 160 */     return (ImapFlag)this.mNames.get(name.toUpperCase());
/*     */   }
/*     */   
/*     */   List<String> listNames(boolean permanentOnly) {
/* 164 */     if (this.mImapNames.isEmpty()) {
/* 165 */       return Collections.emptyList();
/*     */     }
/*     */     
/* 168 */     List<String> names = new ArrayList();
/* 169 */     for (Map.Entry<String, ImapFlag> entry : this.mImapNames.entrySet()) {
/* 170 */       ImapFlag i4flag = (ImapFlag)entry.getValue();
/* 171 */       if ((i4flag.mListed) && ((!permanentOnly) || (i4flag.mPermanent))) {
/* 172 */         names.add(i4flag.mImapName);
/*     */       }
/*     */     }
/* 175 */     return names;
/*     */   }
/*     */   
/*     */   public ImapFlag cache(ImapFlag i4flag) {
/* 179 */     this.mImapNames.put(i4flag.mImapName.toUpperCase(), i4flag);
/* 180 */     this.mNames.put(i4flag.mName.toUpperCase(), i4flag);
/* 181 */     return i4flag;
/*     */   }
/*     */   
/*     */   ImapFlag uncache(int tagId) {
/* 185 */     for (ImapFlag i4flag : this) {
/* 186 */       if (i4flag.mId == tagId) {
/* 187 */         this.mImapNames.remove(i4flag.mImapName.toUpperCase());
/* 188 */         this.mNames.remove(i4flag.mName.toUpperCase());
/* 189 */         return i4flag;
/*     */       }
/*     */     }
/* 192 */     return null;
/*     */   }
/*     */   
/*     */   void clear() {
/* 196 */     this.mImapNames.clear();
/* 197 */     this.mNames.clear();
/*     */   }
/*     */   
/*     */   public Iterator<ImapFlag> iterator()
/*     */   {
/* 202 */     return this.mImapNames.values().iterator();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/ImapFlagCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */